//
//  BwtNativeAd.m
//  bwt_ad_flutter
//
//  Created by 帅 on 2025/6/24.
//

#import "BwtNativeAd.h"
#import <CXHAdSDK-Core/CXHAdSDKNativeAd.h>
#import <CXHAdSDK-Kit/CXHMacro.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Kit/CXHWebView.h>
#import "BwtAdLogManager.h"
#import "BwtAdEventManager.h"
@interface BwtNativeAd()<CXHAdSDKNativeAdDelegate>

@property (nonatomic, strong)CXHAdSDKNativeAd *nativeAd;

@property (nonatomic) bool isAdExposed;
@property (nonatomic) bool isAdClosed;
@end
@implementation BwtNativeAd


- (void)loadNativeAd{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIViewController *rvc = window.rootViewController;
    
    CXHAdSDKNativeAd *nativeAd = [[CXHAdSDKNativeAd alloc]init];
    _nativeAd = nativeAd;
    _nativeAd.delegate = self;
    _nativeAd.controller = rvc;
    _nativeAd.posId = _adParams.posId;
    [_nativeAd load:1];
}
/**
信息流原生广告请求成功
@param nativeAd 广告实例
@param adViewArray 广告模板集合
*/
- (void)CXH_nativeAdSucessToLoad:(CXHAdSDKNativeAd *)nativeAd
                     adViewArray:(NSArray<__kindof
                                  UIView<CXHAdapterNativeAdViewDelegate> *> *)adViewArray{
    UIView<CXHAdapterNativeAdViewDelegate> *adview = [adViewArray firstObject];
    [adview cxh_registViews:@[adview]];
  
    adview.frame = CGRectMake(0, 0, _adParams.adWidth.floatValue, _adParams.adHeight.floatValue);
    
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(0, 0, _adParams.adWidth.floatValue, _adParams.adHeight.floatValue);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [adview addSubview:imageView];
    
    
    CXHAdapterNativeAdData *adData = [adview cxh_nativeAdData];
    if (!kCXHStringIsEmpty(adData.imageUrl)) {
     
        [imageView cxh_setImageWithURL:[NSURL URLWithString:adData.imageUrl]];
//        // 谷歌渠道点击注册方法
//        if ([adview
//             respondsToSelector:@selector(cxh_registerAdMobView:viewType:)]) {
//            [adview cxh_registerAdMobView:imageView
//                                 viewType:CXHAdSDKNativeAdMobAssetTypeImage];
//        }
        
        [[BwtAdLogManager getBwtAdLogManager] log:@"信息流自渲染广告渲染成功, adId: %@", _adParams.adId];
        /// 通知Flutter端
        BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
        NSDictionary *nativeData = @{@"title":adData.title
                                     ,@"desc":adData.desc
                                     ,@"imageUrl":adData.imageUrl
                                     ,@"imageUrlAry":adData.imageUrlAry
                                     ,@"iconImageUrl":adData.iconImageUrl};
        _adParams.nativeData = nativeData;
        
        [bwtAdEventManager postNativeAdOnAdLoadEvent:_adParams];
        
        _nativeAdView = adview;
        NSMutableDictionary *extras = [[NSMutableDictionary alloc] initWithCapacity:0];
        [extras setObject:@(adview.frame.size.width) forKey:@"adWidth"];
        [extras setObject:@(adview.frame.size.height) forKey:@"adHeight"];
        [bwtAdEventManager postOnAdMeasuredEvent:_adParams extras: extras];
    }
   
    [[BwtAdLogManager getBwtAdLogManager] log:@"信息流自渲染广告加载成功, adId: %@", _adParams.adId];
    
}
- (void)closeAd{
    [[BwtAdLogManager getBwtAdLogManager] log:@"信息流自渲染广告被关闭, adId: %@", _adParams.adId];
    self.isAdClosed = YES;
    /// 通知Flutter端
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager postOnAdCloseEvent:_adParams];
    [_nativeAdView cxh_close];
}

/**
 信息流广告请求失败

 @param nativeAd 广告实例
 @param error 请求错误描述
 */
- (void)CXH_nativeAdFailToLoad:(CXHAdSDKNativeAd *)nativeAd
                         error:(NSError *)error{
    NSString *message = [error localizedDescription];
    
    [[BwtAdLogManager getBwtAdLogManager] log:@"信息流自渲染广告加载失败, adId: %@, error: %@", _adParams.adId, message];
    
    /// 通知Flutter端
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager postOnAdLoadFailedEvent:_adParams message:message];
    
    [self releaseAd];
}

/**
 信息流广告被点击

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClicked:(CXHAdSDKNativeAd *)nativeAd
                     adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView loadingPageURL:(NSString *)loadingPageURL{
    [[BwtAdLogManager getBwtAdLogManager] log:@"信息流自渲染广告被点击, adId: %@", _adParams.adId];
    /// 通知Flutter端
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager postOnAdClickEvent:_adParams];
}

/**
 信息流广告被关闭
 
 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClose:(CXHAdSDKNativeAd *)nativeAd
                   adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    if (!self.isAdClosed) {
        [[BwtAdLogManager getBwtAdLogManager] log:@"信息流自渲染广告被关闭, adId: %@", _adParams.adId];
        self.isAdClosed = YES;
        /// 通知Flutter端
        BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
        [bwtAdEventManager postOnAdCloseEvent:_adParams];
    }
    
    [self releaseAd];
}

/**
 信息流广告被展示
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposure:(CXHAdSDKNativeAd *)nativeAd
                      adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    if (!self.isAdExposed) {
        [[BwtAdLogManager getBwtAdLogManager] log:@"信息流自渲染广告被展示, adId: %@", _adParams.adId];
        self.isAdExposed = YES;
        /// 通知Flutter端
        BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
        [bwtAdEventManager postOnAdExposeEvent:_adParams];
    }
}




- (void)releaseAd{
    if (self.nativeAd) {
        self.nativeAd = nil;
        [[BwtAdLogManager getBwtAdLogManager] log:@"广告已释放, adId: %@", _adParams.adId];
    }
}

@end
